package com.info.zhiduoduo.repository.mapper.partner;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.info.zhiduoduo.repository.dto.partner.PartnerStudioWithdrawDto;
import com.info.zhiduoduo.repository.dto.partner.PartnerWithdrawRecordDto;
import com.info.zhiduoduo.repository.entity.partner.PartnerStudioWithdraw;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author bob
 * @since 2020-08-02
 */
public interface PartnerStudioWithdrawMapper extends BaseMapper<PartnerStudioWithdraw> {

	@Select(" select psw.*,pa.realname AS partnerName,ms.userid,ms.username,ms.invoicescope,ms.specialinvoice " +
			" from zdd_partner_studio_withdraw psw " +
			" join zdd_partner_account pa on pa.partnerid = psw.partnerid " +
			" join zdd_maker_studio ms on ms.userid = pa.studio_id " +
			" where CONCAT(pa.realname,ms.username) like '%${name}%' " +
			" and if(#{state} = 0, 1=1, psw.state = #{state}) " +
			" and if(ISNULL(#{from}), 1=1, DATE(psw.createtime) >= DATE(#{from})) " +
			" and if(ISNULL(#{to}), 1=1, DATE(psw.createtime) <= DATE(#{to})) " +
			" order by psw.state,psw.id desc ")
	List<PartnerStudioWithdrawDto> list(@Param("name") String name, @Param("state") int state, @Param("from") Date from, @Param("to") Date to);

	@Select(" select psw.*,pa.realname AS partnerName,ms.userid,ms.username,ms.invoicescope,ms.specialinvoice " +
			" from zdd_partner_studio_withdraw psw " +
			" join zdd_partner_account pa on pa.partnerid = psw.partnerid " +
			" join zdd_maker_studio ms on ms.userid = pa.studio_id " +
			" where psw.partnerid = #{partnerid} " +
			" and if(#{state} = 0, 1=1, psw.state = #{state}) " +
			" and if(ISNULL(#{from}), 1=1, DATE(psw.createtime) >= DATE(#{from})) " +
			" and if(ISNULL(#{to}), 1=1, DATE(psw.createtime) <= DATE(#{to})) " +
			" order by psw.state,psw.id desc ")
	List<PartnerStudioWithdrawDto> listByPartnerid(@Param("partnerid") int partnerid, @Param("state") int state, @Param("from") Date from,
	                                    @Param("to") Date to);

	@Select(" select psw.id,psw.partnerid,psw.amount,psw.state,psw.createtime,2 AS remitType, pa.realname " +
			" from zdd_partner_studio_withdraw psw " +
			" join zdd_partner_account pa on pa.partnerid = psw.partnerid " +
			" where psw.partnerid = #{userid} " +
			" order by id desc ")
	List<PartnerWithdrawRecordDto> listWithdrawRecord(int userid);

	@Select(" select GROUP_CONCAT(pa.realname) " +
			" from zdd_partner_studio_withdraw psw " +
			" join zdd_partner_account pa on pa.partnerid = psw.partnerid " +
			" where psw.id in (#{ids}) ")
	String partnerNameByIds(String ids);

	@Select(" select COUNT(*) from zdd_partner_studio_withdraw where state = 1")
	int countApplyingWithdraws();
}
